/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Edge;
import evaluation.Graph;
import evaluation.Vertex;
import java.util.LinkedHashSet;
import java.util.Map;
import logic.Type;

/**
 *
 * @author phong
 */
public class DRSEqualityCondition extends DRSBasicCondition {
    
    DrtEntity first, second;
    
    public DRSEqualityCondition(DrtEntity e1, DrtEntity e2) {
        this.first = e1;
        this.second = e2;
    }
    
    public DrtEntity getFirst() {
        return this.first;
    }
    
    public DrtEntity getSecond() {
        return this.second;
    }
    
    @Override
    public DRSEqualityCondition simplify() {
        return this; //new DRSEqualityCondition(this.first.simplify(), this.second.simplify());
    }
    
    @Override
    public DRSEqualityCondition replace(DrtVariable var, DrtExpression exp) {
        return new DRSEqualityCondition(
                this.first.replace(var, exp), 
                this.second.replace(var, exp));
    }
    
    @Override
    public Type getType() {
        return Type.TRUTH_TYPE;
    }
    
    @Override
    public void setType(Type t) {}
    
    @Override
    public void getAllVariables(LinkedHashSet<DrtVariable> vars) {
        this.first.getAllVariables(vars);
        this.second.getAllVariables(vars);
    }

    @Override
    public void getRefs(LinkedHashSet<DRSReferent> refs) {
        this.first.getRefs(refs);
        this.second.getRefs(refs);
    }
    
    @Override
    public String toString() {
        return this.first.toString() + "=" + this.second.toString();
    }
    
    @Override
    public boolean equals(Object other){
        if (other instanceof DRSEqualityCondition) {
            DRSEqualityCondition that = (DRSEqualityCondition)other;
            return this.first.equals(that.first) && this.second.equals(that.second);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return "equal".hashCode(); // this.first.hashCode() * 31 + this.second.hashCode();
    }
    
    @Override
    public Graph toGraph() {
        Graph g = new Graph();
        
        if (first instanceof DrtCardinality) {
            Vertex v = new Vertex(((DrtCardinality)first).getArg().getName(), Vertex.TYPE_REF);
            Vertex f = new Vertex("card = " + this.second.toString(), Vertex.TYPE_PRED);
            g.addEdge(new Edge("", v, f));
            g.addMarks(f);
        }
        else {
            Vertex v1 = new Vertex(this.first.getName(), Vertex.TYPE_REF);
            Vertex v2 = new Vertex(this.second.getName(), Vertex.TYPE_REF);
            Vertex eq = Vertex.createEq();
            g.addEdge(new Edge("", v1, eq));            
            g.addEdge(new Edge("", v2, eq));            
            g.addMarks(eq);
        }
        
        return g;
    }
    
    @Override
    public DRSEqualityCondition getTemplate(Map<String,String> map) {
        DrtEntity newFirst = this.first.getTemplate(map);
        DrtEntity newSecond = this.second.getTemplate(map);
        return new DRSEqualityCondition(newFirst, newSecond);
    }
}
